//
//  XMNavigationController.m
//  XiaoMiao
//
//  Created by 23 on 2017/11/4.
//  Copyright © 2017年 smallyou. All rights reserved.
//

#import "XMNavigationController.h"
#import "XMHomeController.h"
#import "XMNavigationController.h"
#import "LoginController.h"

@interface XMNavigationController () <UINavigationControllerDelegate,UIGestureRecognizerDelegate>

@end

@implementation XMNavigationController

/**
 *  当前类初始化的时候才会调用
 *  作用:初始化类
 */
+ (void)initialize {
    //
    if (self == [XMNavigationController class]) {
        // 获取当前类下面的导航条 
        UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[self]];
        
        [navBar setBackgroundImage:[UIImage app_createImageWithColor:[UIColor whiteColor] size:kSize(100, 100)] forBarMetrics:UIBarMetricsDefault];
//        [navBar setShadowImage:[[UIImage alloc] init]];
        // 标题颜色
        NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
        dictM[NSForegroundColorAttributeName] = HEXPColor(0x2C2E30, 1.0);
        dictM[NSFontAttributeName] = [UIFont boldSystemFontOfSize:19.0f];
        [navBar setTitleTextAttributes:dictM];
        
        //
        navBar.tintColor = kRGBColor(51, 51, 51);
        
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    }
}

- (void)viewDidLoad {
    //恢复系统的侧滑返回手势---由于全屏返回手势与TableViewCell的左滑删除手势冲突，取消全屏返回手势
    self.interactivePopGestureRecognizer.delegate = self;
    
    //
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    dictM[NSFontAttributeName] = [UIFont systemFontOfSize:9];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:dictM forState:UIControlStateNormal];
    [self.navigationItem.leftBarButtonItem setTitleTextAttributes:dictM forState:UIControlStateNormal];
    
    // 
    [self.navigationBar setShadowImage:[UIImage app_createImageWithColor:kRGBColor(220, 220, 220) size:CGSizeMake(kScreenWidth, 0.5)]];
 
    //  3 监听通知
    [self setupNotification];
}

#pragma mark - UIGestureRecognizerDelegate - 恢复系统的侧滑返回手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (self.childViewControllers.count < 2) {
        return NO;
    }
    
//    return YES;
    return NO;
}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断下是否是根控制器
    if (self.childViewControllers.count != 0) { // 非根控制器
        // 设置导航条左边的按钮
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_common_arrow_back_black"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
        
    }
    
    [super pushViewController:viewController animated:animated];
}

- (void)back {
    
    [self popViewControllerAnimated:YES];
    
    //预约完成后点击完成会出现系统的tab遮住自定义的tab的情况，需要在back方法中调用下面的代码
    for (UIView *tabBar in self.tabBarController.tabBar.subviews) {
        if ([tabBar isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBar removeFromSuperview];
        }
    }
    
    //恢复导航栏背景颜色
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[self.class]];
    [navBar setBackgroundImage:[UIImage app_createImageWithColor:[UIColor whiteColor] size:kSize(100, 100)] forBarMetrics:UIBarMetricsDefault];
}

/**
 *  这个方法是防止 pop 后 UITabBarButton 重叠的  ======== 有问题：该方法不调用，预约完成后点击完成会出现系统的tab遮住自定义的tab的情况，需要在back方法中调用下面的代码
 */
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    
    // 删除系统自带的tabBarButton
    for (UIView *tabBar in self.tabBarController.tabBar.subviews) {
        if ([tabBar isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBar removeFromSuperview];
        }
    }
}


/**监听通知*/
- (void)setupNotification
{
    [AppNotificationHelpper addObserver:self selector:@selector(actionLoginTimeout) name:kLoginSessionTimeoutNotification object:nil];
}



#pragma mark - 监听通知
- (void)actionLoginTimeout
{
    //清除登录信息
    [AppGlobal logout];
    
    [self popToRootViewControllerAnimated:YES];
    
    //登录会话超时
    WEAKSELF

    LoginController *login = [[LoginController alloc] init];
    [login setLoginSuccessBlock:^(LoginInfoModel *loginInfoModel) {
        [self dismissViewControllerAnimated:YES completion:nil];

    }];
    
    NSMutableDictionary *headparam = [NSMutableDictionary dictionary];
    headparam[@"deviceToken"] = [AppGlobal getDeviceToken];
    [AppNetwork configCommonHttpHeaders:headparam];
    
    XMNavigationController *loginNav = [[XMNavigationController alloc] initWithRootViewController:login];
    [weakSelf presentViewController:loginNav animated:NO completion:nil];
    
    

    
    
    [KLProgressHUD showPromptHUDWithSuperView:[UIApplication sharedApplication].keyWindow message:@"您的帐号在另一台设备上登陆" andTime:2.0 completed:^{

    }];
    
}
@end
